<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Eigen: Reshape and Slicing</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(function() { init_search(); });
/* @license-end */
</script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="eigendoxy.css" rel="stylesheet" type="text/css">
<!--  -->
<script type="text/javascript" src="eigen_navtree_hacks.js"></script>
</head>
<body>
<div style="background:#FFDDDD;font-size:120%;text-align:center;margin:0;padding:5px">Please, help us to better know about our user community by answering the following short survey:  <a href="https://forms.gle/wpyrxWi18ox9Z5ae9">https://forms.gle/wpyrxWi18ox9Z5ae9</a></div>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="Eigen_Silly_Professor_64x64.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="http://eigen.tuxfamily.org">Eigen</a>
   &#160;<span id="projectnumber">3.3.9</span>
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.svg"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('group__TutorialReshapeSlicing.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Reshape and Slicing<div class="ingroups"><a class="el" href="group__DenseMatrixManipulation__chapter.html">Dense matrix and array manipulation</a></div></div>  </div>
</div><!--header-->
<div class="contents">
<p>Eigen does not expose convenient methods to take slices or to reshape a matrix yet. Nonetheless, such features can easily be emulated using the <a class="el" href="classEigen_1_1Map.html" title="A matrix or vector expression mapping an existing array of data.">Map</a> class.</p>
<h1><a class="anchor" id="TutorialReshape"></a>
Reshape</h1>
<p>A reshape operation consists in modifying the sizes of a matrix while keeping the same coefficients. Instead of modifying the input matrix itself, which is not possible for compile-time sizes, the approach consist in creating a different <em>view</em> on the storage using class <a class="el" href="classEigen_1_1Map.html" title="A matrix or vector expression mapping an existing array of data.">Map</a>. Here is a typical example creating a 1D linear view of a matrix:</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line">MatrixXf M1(3,3);    <span class="comment">// Column-major storage</span></div>
<div class="line">M1 &lt;&lt; 1, 2, 3,</div>
<div class="line">      4, 5, 6,</div>
<div class="line">      7, 8, 9;</div>
<div class="line"> </div>
<div class="line">Map&lt;RowVectorXf&gt; v1(M1.data(), M1.size());</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;v1:&quot;</span> &lt;&lt; endl &lt;&lt; v1 &lt;&lt; endl;</div>
<div class="line"> </div>
<div class="line">Matrix&lt;float,Dynamic,Dynamic,RowMajor&gt; M2(M1);</div>
<div class="line">Map&lt;RowVectorXf&gt; v2(M2.data(), M2.size());</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;v2:&quot;</span> &lt;&lt; endl &lt;&lt; v2 &lt;&lt; endl;</div>
</div><!-- fragment -->  </td><td><pre class="fragment">v1:
1 4 7 2 5 8 3 6 9
v2:
1 2 3 4 5 6 7 8 9
</pre> </td></tr>
</table>
<p>Remark how the storage order of the input matrix modifies the order of the coefficients in the linear view. Here is another example reshaping a 2x6 matrix to a 6x2 one: </p><table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line">MatrixXf M1(2,6);    <span class="comment">// Column-major storage</span></div>
<div class="line">M1 &lt;&lt; 1, 2, 3,  4,  5,  6,</div>
<div class="line">      7, 8, 9, 10, 11, 12;</div>
<div class="line"> </div>
<div class="line">Map&lt;MatrixXf&gt; M2(M1.data(), 6,2);</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;M2:&quot;</span> &lt;&lt; endl &lt;&lt; M2 &lt;&lt; endl;</div>
</div><!-- fragment -->  </td><td><pre class="fragment">M2:
 1  4
 7 10
 2  5
 8 11
 3  6
 9 12
</pre> </td></tr>
</table>
<h1><a class="anchor" id="TutorialSlicing"></a>
Slicing</h1>
<p>Slicing consists in taking a set of rows, columns, or elements, uniformly spaced within a matrix. Again, the class <a class="el" href="classEigen_1_1Map.html" title="A matrix or vector expression mapping an existing array of data.">Map</a> allows to easily mimic this feature.</p>
<p>For instance, one can skip every P elements in a vector: </p><table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line">RowVectorXf v = <a class="code" href="classEigen_1_1DenseBase.html#af0dd2c2ee796c50a63126e1e87b55985">RowVectorXf::LinSpaced</a>(20,0,19);</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;Input:&quot;</span> &lt;&lt; endl &lt;&lt; v &lt;&lt; endl;</div>
<div class="line">Map&lt;RowVectorXf,0,InnerStride&lt;2&gt; &gt; v2(v.data(), v.size()/2);</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;Even:&quot;</span> &lt;&lt; v2 &lt;&lt; endl;</div>
<div class="ttc" id="aclassEigen_1_1DenseBase_html_af0dd2c2ee796c50a63126e1e87b55985"><div class="ttname"><a href="classEigen_1_1DenseBase.html#af0dd2c2ee796c50a63126e1e87b55985">Eigen::DenseBase::LinSpaced</a></div><div class="ttdeci">static const SequentialLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &amp;low, const Scalar &amp;high)</div><div class="ttdef"><b>Definition:</b> CwiseNullaryOp.h:224</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">Input:
 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19
Even: 0  2  4  6  8 10 12 14 16 18
</pre> </td></tr>
</table>
<p>One can olso take one column over three using an adequate outer-stride or inner-stride depending on the actual storage order: </p><table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line">MatrixXf M1 = <a class="code" href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">MatrixXf::Random</a>(3,8);</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;Column major input:&quot;</span> &lt;&lt; endl &lt;&lt; M1 &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="line">Map&lt;MatrixXf,0,OuterStride&lt;&gt; &gt; M2(M1.data(), M1.rows(), (M1.cols()+2)/3, OuterStride&lt;&gt;(M1.outerStride()*3));</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;1 column over 3:&quot;</span> &lt;&lt; endl &lt;&lt; M2 &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="line"> </div>
<div class="line"><span class="keyword">typedef</span> Matrix&lt;float,Dynamic,Dynamic,RowMajor&gt; RowMajorMatrixXf;</div>
<div class="line">RowMajorMatrixXf M3(M1);</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;Row major input:&quot;</span> &lt;&lt; endl &lt;&lt; M3 &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="line">Map&lt;RowMajorMatrixXf,0,Stride&lt;Dynamic,3&gt; &gt; M4(M3.data(), M3.rows(), (M3.cols()+2)/3,</div>
<div class="line">                                              Stride&lt;Dynamic,3&gt;(M3.outerStride(),3));</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;1 column over 3:&quot;</span> &lt;&lt; endl &lt;&lt; M4 &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="ttc" id="aclassEigen_1_1DenseBase_html_ae814abb451b48ed872819192dc188c19"><div class="ttname"><a href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">Eigen::DenseBase::Random</a></div><div class="ttdeci">static const RandomReturnType Random()</div><div class="ttdef"><b>Definition:</b> Random.h:113</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">Column major input:
   0.68   0.597   -0.33   0.108   -0.27   0.832  -0.717  -0.514
 -0.211   0.823   0.536 -0.0452  0.0268   0.271   0.214  -0.726
  0.566  -0.605  -0.444   0.258   0.904   0.435  -0.967   0.608
1 column over 3:
   0.68   0.108  -0.717
 -0.211 -0.0452   0.214
  0.566   0.258  -0.967
Row major input:
   0.68   0.597   -0.33   0.108   -0.27   0.832  -0.717  -0.514
 -0.211   0.823   0.536 -0.0452  0.0268   0.271   0.214  -0.726
  0.566  -0.605  -0.444   0.258   0.904   0.435  -0.967   0.608
1 column over 3:
   0.68   0.108  -0.717
 -0.211 -0.0452   0.214
  0.566   0.258  -0.967
</pre> </td></tr>
</table>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Wed Aug 18 2021 14:16:26 for Eigen by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li>
  </ul>
</div>
</body>
</html>
